package com.example.demo.mq;


import org.apache.activemq.artemis.api.core.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Second {
    private static final Logger logger = LoggerFactory.getLogger(Second.class);

    public static void main(String[] args) {
        ServerLocator serverLocator = null;
        ClientSessionFactory factory = null;
        String queueName = "jinxin_queue_00";
        try {
            serverLocator = ActiveMQClient.createServerLocator("tcp://127.0.0.1:61616");
            //serverLocator.setPreAcknowledge(Boolean.TRUE);
            factory = serverLocator.createSessionFactory();
            ClientSession session = factory.createSession();
            session.start();
            ClientConsumer consumer = session.createConsumer(queueName);
            ClientMessage message = consumer.receiveImmediate();
            message.acknowledge();
            logger.debug("Reply from Server Msg1: " + message.getBodyBuffer().readString());

//            ClientMessage message2 = consumer.receive();
//            logger.debug("Reply from Server Msg2: " + message2.getBodyBuffer().readString());
//
//            ClientMessage message3 = consumer.receive();
//            logger.debug("Reply from Server Msg3: " + message3.getBodyBuffer().readString());
//
//            ClientMessage message4 = consumer.receive();
//            logger.debug("Reply from Server Msg4: " + message4.getBodyBuffer().readString());

            session.close();
        } catch (Exception e) {
            logger.error("Failed to create client session factory, reason: {}", e.getMessage());
        } finally {
            if(factory != null) {
                factory.close();
            }
        }
    }
}
